/** 用户相关信息
 * 1. 系统中已登录过的用户（会员）
 * 2. 当前正在使用的用户
 * 3. 当前用户状态：锁屏、关机（清除当前登录状态）、删除用户（在会员中清除当前用户）
 * */
const user = {
  state (){
    return {
      members: {}, // 系统中已登录过的用户 localStorage
      user: null, // 当前用户 sessionStorage
      isLock: false, // 是否锁屏 sessionStorage
    }
  },
  mutations: {
    /** 会员操作 - 追加会员
     * @param {Object} data - 会员信息
     * */
    addMember: (state, data) => {
      state.members[data.loginName] = data;
      localStorage.setItem('members', JSON.stringify(state.members));
    },
    /** 会员操作-按名称删除会员
     * @param {String} loginName - 会员名称
     * */
    removeMember: (state, loginName) => {
      delete state.members[loginName];
      localStorage.setItem('members', JSON.stringify(state.members));
      this.user(state, null);
    },
    /** 用户操作
    * @param {Object} data - 设置用户信息
    * @param {null} data - 注销
    * */
    user: (state, data) => {
      state.user = data;
      localStorage.setItem('user', JSON.stringify(data));
    },
    /** 锁屏状态
     * @param {Boolean} data - false 解锁  | true 锁屏
     * */
    isLock: (state, data) => {
      state.isLock = data;
      localStorage.setItem('isLock', JSON.stringify(data));
    },
  },
  getters: {
    members: (state) => {
      if (!state.members) {
        state.members = JSON.parse(localStorage.getItem('members'));
      }
      return state.members;
    },
    user: (state) => {
      if (!state.user) {
        state.user = JSON.parse(localStorage.getItem('user'));
      }
      return state.user;
    },
    isLock: (state) => {
      if (!state.isLock) {
        state.isLock = JSON.parse(localStorage.getItem('isLock'));
      }
      return state.isLock;
    },

  }
};
export default user;